package com.jianyun.jyzs.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.EnNatureAdapter;
import com.jianyun.jyzs.bean.EnNatureResponse;
import com.jianyun.jyzs.common.ITextWatchListener;
import com.jianyun.jyzs.dao.EnNatureDao;
import com.jianyun.jyzs.http.EnNatureHelper;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jrmf360.rylib.common.util.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yangshijun on 2018/6/4.
 * 创建CRM--->选择企业性质
 */

public class EntNatureActivity extends AppCompatActivity implements EnNatureHelper.EnNatureListener {
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.tv_checked)
    TextView tvChecked;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.iv_moreLinearLayout)
    LinearLayout ivMoreLinearLayout;
    @BindView(R.id.topCreat)
    LinearLayout topCreat;
    @BindView(R.id.iv_single)
    ImageView ivSingle;
    @BindView(R.id.iv_group)
    ImageView ivGroup;
    @BindView(R.id.topChatDetail)
    LinearLayout topChatDetail;
    @BindView(R.id.topId)
    RelativeLayout topId;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.linearLayoutMenu)
    LinearLayout linearLayoutMenu;
    @BindView(R.id.horizonMenu)
    HorizontalScrollView horizonMenu;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private EnNatureAdapter natureAdapter;
    private EnNatureDao natureDao;
    private String enterpriseCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_ennature_list);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        topBack.setVisibility(View.VISIBLE);
        topText.setVisibility(View.VISIBLE);
        tvChecked.setVisibility(View.VISIBLE);
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        topText.setText("选择企业性质");
        tvChecked.setText("刷新");
        tvChecked.setTextColor(getResources().getColor(R.color.blue));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        natureAdapter = new EnNatureAdapter(this, null);
        recyclerView.setAdapter(natureAdapter);
        natureDao = new EnNatureDao(this);
        etSearch.addTextChangedListener(new ITextWatchListener() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String trim = s.toString().trim();
                if (trim != null) {
                    List<EnNatureResponse.EnNatureBean> enNatureSearch = natureDao.getEnNatureSearch(enterpriseCode, trim);
                    if (enNatureSearch != null) natureAdapter.setList(enNatureSearch);
                }

            }
        });
        EnNatureHelper.getNewEnNatureList(enterpriseCode, this);
    }

    @OnClick({R.id.topBack, R.id.tv_checked})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.topBack:
                finish();
                break;
            case R.id.tv_checked:
                EnNatureHelper.getNewEnNatureList(enterpriseCode, this);
                break;
        }
    }

    @Override
    public void getSuccess(List<EnNatureResponse.EnNatureBean> list) {
        if (list != null && list.size() > 0) {
            natureAdapter.setList(list);
            natureDao.clear(enterpriseCode);
            for (EnNatureResponse.EnNatureBean l : list) {
                natureDao.insert(l, enterpriseCode);
            }
        } else {
            ToastUtil.showNoWaitToast(this, "没有数据");
        }
    }

    @Override
    public void getFail(String msg) {
        ToastUtil.showNoWaitToast(this, msg);
    }
}
